﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Runtime.Serialization;

namespace CMD.BAL.CAT
{
    [DataContractAttribute]
    public class Clo
    {
        [DataMemberAttribute(EmitDefaultValue = false)]
        public int idClo;
        [DataMemberAttribute(EmitDefaultValue = false)]
        public string dscClo;
        [DataMemberAttribute(EmitDefaultValue = false)]
        public string codigo;
        [DataMemberAttribute(EmitDefaultValue = false)]
        public string dscYVentas;
        public static DataTable GetData()
        {
            DataTable dt;
            //ejecutar store
            dt = CMD_WEB.DAL.SrvDB.ExecuteQuery("exec SP_CMD_CAT_SEL_CLO");
            return dt;
        }
        public static DataTable GetDataIdClo(int idClo)
        {
            DataTable dt;
            //ejecutar store
            dt = CMD_WEB.DAL.SrvDB.ExecuteQuery("exec SP_CMD_CAT_SEL_CLO_WEB'" + idClo + "'");
            return dt;
        }
        public static List<Clo> GetDataClo()
        {
            try
            {
                DataTable dt;
                dt = Clo.GetData();
                if (dt.Rows.Count == 0)
                {
                    return null;
                }
                else
                {
                    List<Clo> list = dt.AsEnumerable().Select(
                    row => new Clo
                    {
                        idClo = row.Field<int?>("ID_CLO").GetValueOrDefault(),
                        dscClo = String.IsNullOrEmpty(row.Field<string>("DSC_CLO"))
                        ? ""
                        : row.Field<string>("DSC_CLO"),
                        codigo = String.IsNullOrEmpty(row.Field<string>("CODIGO"))
                        ? ""
                        : row.Field<string>("CODIGO"),
                        dscYVentas = String.IsNullOrEmpty(row.Field<string>("DSCyVENTA"))
                        ? ""
                        : row.Field<string>("DSCyVENTA"),

                    }
                    ).ToList();
                    return list;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public static Clo GetDataCloCodigo(int idClo)
        {
            try
            {
                Clo cl;
                DataTable dt = Clo.GetDataIdClo(idClo);
                if (dt.Rows.Count == 0)
                {
                    return null;
                }
                else if (idClo == 0)
                {
                    return null;
                }
                else
                {
                    cl = new Clo(0,null,Convert.ToString(dt.Rows[0]["datRFV_codigo"]), null);
                }
                return cl;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public Clo()
        {
            idClo = 0;
            dscClo="";
            codigo="";
            dscYVentas = "";
        }
        public Clo(int idClo, string dscClo, string codigo, string dscYVentas)
        {
            this.idClo = idClo;
            this.dscClo = dscClo;
            this.codigo = codigo;
            this.dscYVentas = dscYVentas;
        }
    }
}